home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: netcom.com!cman
- From: cman@netcom.com (Mike Austin)
- Subject: Re: [Q] Best way to "define" flag values?
- Message-ID: <cmanDLKAqG.Drt@netcom.com>
- Organization: NETCOM On-line Communication Services (408 261-4700 guest)
- X-Newsreader: TIN [version 1.2 PL1]
- References: <marnoldDLIv9w.1s4@netcom.com>
- Distribution: na
- Date: Mon, 22 Jan 1996 02:54:16 GMT
- Sender: cman@netcom.netcom.com
-
- Here's a little essay that I did a while ago. I still think the same
- way now, but my views have broadened...
-
- Enumerations and Parameterless Macros
-
- Enumerations have always been used in C and C++, but there are
- different opinions on whether they should be used or not. As
- enumerations provide the ability to use symbolic constants, so does the
- #define statement. So what are the major differences of the two?
- Enumerations are more like a data-type; you can nest enumerations in
- classes and structures, helping in supporting encapsulation.
- Enumerations are less dependent on the actual value represented; if you
- insert a new enumeration constant, you don't have to declare the value
- associated with it. Because it helps support encapsulation, there are
- less names used in the global name-space.
-
- Parameterless macros (#define XXX YYY) have been used in C since it's
- inception, and has made its way to C++ code although mostly used for
- pseudo-templates and other hacky uses. Still, if you would remove
- support for these, many C++ programs would break. The obvious breakage
- would be because of C++'s lack of a smart include file handling. Not
- only is it tedious and takes up space to write:
-
- #ifndef FILE_H
- #define FILE_H
- ...
- #endif
-
- but the file must be opened each time, and read to the end of the file.
- Not to get off track, but it is also inefficient in that it has to
- compile the included file every time. Enumerations are clearly the ones
- to use in C++ code, but in my eyes, they could be improved. First off,
- #defines have been used for defining bit sets for a long time.
- Enumerations can also handle the task, but you must specify the values to
- get the desired result. For example:
-
- enum Style { NoTitle = 0x0001, NoBorder = 0x0002, NoResize = 0x0004};
-
- must be spelled out explicitly to work correctly. I find it would make
- programming much easier if there was a keyword 'enumbit' which performed
- the above with the more simple:
-
- enum Style { NoTitle, NoBorder, NoResize };
-
- not only is it easier to read, it avoids mistakes like creating
- duplicates, misplacements, etc. I see one other improvement that might
- not gain much popularity, but reduce source code size and redundancy.
- Say you have class Window which contains the enumeration described above.
-
- class Window {
- public:
- Window(Style aStyle);
- enumbit Style { NoTitle, NoBorder, NoResize };
- };
-
- create a window with all three window styles set:
-
- win(Window::NoTitle | Window::NoBorder | Window::NoResize);
-
- not very elegant. Why would you have to use scoping when the compiler
- knows the argument must be of type Style? Some people may say it would
- degrade the consistency of c++'s scope mechanism, but I feel it
- over-weighs the argument. Some people also say that they don't program
- in this fashion, and they don't use enumerations or macros at all.
-
- --
- ------------------------------------------------------------------------------
-
- "I just got paid today, got me a pocket full of change"
-
- <A HREF="ftp://ftp.netcom.com/pub/cm/cman/index.html">Mike's Home Page</A>
- Links to FAQs; cool commercial, graphics and music pages, etc.
-